/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpdisenio2009.model.dao;

import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import tpdisenio2009.model.HibernateUtils;
import tpdisenio2009.model.beans.TournamentDate;

/**
 *
 * @author Pablito
 */
public class TournamentDateDAO extends AbstractDAO<TournamentDate> {

    @Override
    public Class<TournamentDate> getPersistentClass() {
        return TournamentDate.class;
    }

    public List<TournamentDate> getClosedTournamentDates() {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( TournamentDate.class );
        mainCriteria.add( Restrictions.eq( "state", TournamentDate.CLOSED_DATE ) );
        
        return mainCriteria.list();
    }

    public List<TournamentDate> getAllOrdered() {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( TournamentDate.class );
        mainCriteria.add( Restrictions.isNotNull( "initialDate" ) );
        mainCriteria.addOrder( Order.asc( "initialDate" ) );

        return mainCriteria.list();
    }

    /*
    public List<TournamentDate> getAllOrdered() {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( TournamentDate.class );
        mainCriteria.addOrder( Order.asc( "initialDate" ) );

        return mainCriteria.list();
    } */

    /*public TournamentDate getCurrentDate() {
        List<TournamentDate> dates = getAllOrdered();

        for (TournamentDate date : dates)
            if (date.getBanDate() == null)
                return date;

        return null;
    }*/

    public TournamentDate getCurrentDate() {
        List<TournamentDate> dates = getAllOrdered();

        for (TournamentDate date : dates) {
            if ( date.getState().equals( TournamentDate.OPEN_DATE ) ) {
                return date;
            }
        }
        return dates.get( dates.size() -1 );
    }

    public List<TournamentDate> getAssignableDates() {
        List<TournamentDate> dates = getAllOrdered();

        Iterator<TournamentDate> it = dates.iterator();
        while (it.hasNext()) {
            if (it.next().getBanDate() == null)
                it.remove();
        }

        return dates;
    }
}
